﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication.Models;
using DAO;

namespace WebApplication.Controllers
{
    public class NotificationsController : Controller
    {
        DataManager dm;
        public NotificationsController()
        {
            dm = new DataManager();
        }

        public ActionResult List(string search)
        {
            if(!string.IsNullOrEmpty(search))
                return View(dm.NotificationsManager.GetAll().Where(t => t.Name.Contains(search)));
            return View(dm.NotificationsManager.GetAll());
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(NotificationModel notification)
        {
            if (!ModelState.IsValid)
            {
                ViewData["errors"] = "Объект не валиден";
                return View(notification);
            }
            dm.NotificationsManager.Add(NotificationModel.ToEfEntity(notification));
            dm.SaveChanges();
            return RedirectToAction("List");
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Edit(int id)
        {
            return View(dm.NotificationsManager.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(Notification notification)
        {
            if (!ModelState.IsValid)
            {
                ViewData["errors"] = "Объект не валиден";
                return View(notification);
            }
            dm.NotificationsManager.Edit(notification);
            dm.SaveChanges();
            return RedirectToAction("List");
        }

        public ActionResult Delete(int id)
        {
            dm.NotificationsManager.Delete(id);
            dm.SaveChanges();
            return RedirectToAction("List");
        }

        public ActionResult GetNotificationsUsers()
        {
            return View(dm.NotificationsManager.GetAll());
        }

    }
}
